package org.glut.backend.controller;

import org.glut.backend.domain.Comment;
import org.glut.backend.service.ICommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;

import java.util.List;


@Slf4j
@RestController
@RequestMapping("/public/comment")
public class CommentController {
    @Autowired
    private ICommentService commentService;

    // 修正：查询目标ID下的所有评论（含层级结构）
    @GetMapping("/query/{targetId}")
    public ResponseEntity<List<Comment>> getCommentsByTargetId(@PathVariable Long targetId) { // 参数类型改为Long
        List<Comment> comments = this.commentService.getCommentsByTargetId(targetId); // 调用正确的方法
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("token", "kun");
        return new ResponseEntity<>(comments, httpHeaders, HttpStatus.OK); // 返回值改为List<Comment>
    }

    // 修正：使用接口定义的addComment方法
    @PostMapping
    public ResponseEntity<String> create(@RequestBody Comment comment) {
        log.info("添加评论：{}",comment);
        boolean isSave = this.commentService.addComment(comment); // 调用接口方法
        log.info("保存是否成功：{}", isSave);
        return ResponseEntity.ok().body("添加成功");
    }
/*
    @PutMapping
    public ResponseEntity<String> update(@RequestBody Comment comment) {
        log.info("修改评论：{}", comment);
        boolean isUpdated = commentService.updateComment(comment);
        if (isUpdated) {
            return ResponseEntity.ok("修改成功");
        }
        return ResponseEntity.status(HttpStatus.FORBIDDEN).body("修改失败（无权限或评论不存在）");
    }
*/

    // 新增：删除评论接口
    @DeleteMapping("/{id}")
    public ResponseEntity<String> delete(@PathVariable Long id) {
        log.info("删除评论：{}", id);
        boolean isDeleted = commentService.deleteComment(id);
        return isDeleted ? ResponseEntity.ok("删除成功") : ResponseEntity.status(500).body("删除失败");
    }
}